#include<iostream>
#include<pthread.h>
#include<unistd.h>
#include"RingQueue.hpp"
#include"Task.hpp"

//环形队列
struct ThreadData
{
    RingQueue<Task>*q;
    std::string threadname;
};

void* Productor(void* args)
{
    RingQueue<Task>* rq=static_cast<RingQueue<Task>*>(args);
    int len=opers.size();
    while(true)
    {
        int data1=rand()%10+1;//
        int data2=rand()%10+1;//
        usleep(10);
        char op=opers[rand()%len];//
        Task t(data1,data2,op);//
        //生产数据
        rq->push(t);
        std::cout<<"productor done,data is: "<<t.GetTask()<<std::endl;
        sleep(1);
    }
    return nullptr;
}
void* Consumer(void* args)
{
    RingQueue<Task>* rq=static_cast<RingQueue<Task>*>(args);
    while(true)
    {
    
        int data=0;//
        Task t;
        rq->pop(&t);
        //rq->pop(&data);

        t();
        std::cout<<"consumer get task ,task is "<<t.GetTask()<<std::endl;
        sleep(1);
    }
}
int main()
{
    RingQueue<Task>* rq=new RingQueue<Task>();
    pthread_t p[3] ,c[5];
    // pthread_create(&p,nullptr,Productor,rq);
    // pthread_create(&c,nullptr,Consumer,rq);

    // pthread_join(p,nullptr);
    // pthread_join(c,nullptr);
    for(int i=0;i<5;i++)
    {
        ThreadData* td=new ThreadData();
        td->q=rq;
        td->threadname="productor="+std::to_string(i);
        pthread_create(p+i,nullptr,Productor,td);

    }
    for(int i=0;i<3;i++)
    {
        ThreadData* td=new ThreadData();
        td->q=rq;
        td->threadname="consumer="+std::to_string(i);
        pthread_create(c+i,nullptr,Consumer,td);

    }
    for(int i=0;i<5;i++)
    {
        pthread_join(c[i],nullptr);
    }
    
     for(int i=0;i<3;i++)
    {
        pthread_join(p[i],nullptr);
    }



}